package com.minxi.web.dao;

import com.minxi.web.javabean.Userinfo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class UserDaoImpl implements UserDao{
    @Override
    public Boolean add(Userinfo userinfo) {
        Boolean flag = false;
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        //2.请求数据库 JDBC
        try {
            //1.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获取连接
            String url = "jdbc:mysql://localhost:3306/student?serverTimezone=Asia/Shanghai";
            String user = "root";
            String pwd = "123456";
            connection = DriverManager.getConnection(url, user, pwd);
            //3.编辑SQL语句 (预编译的执行对象)
            String sql = "insert into user_info(user_name,user_pwd,user_phone,user_sex)values(?,?,?,?);";
            //得到执行对象
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,userinfo.getUserName());
            preparedStatement.setString(2,userinfo.getPassWord());
            preparedStatement.setString(3,userinfo.getUserPhone());
            preparedStatement.setInt(4,userinfo.getUserSex());
            int rs = preparedStatement.executeUpdate();
            if(rs > 0){
                flag = true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(null != preparedStatement){
                    preparedStatement.close();
                }
                if(null != connection){
                    connection.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return flag;
    }
    }

